$(function () {
    var commonHost = 'http://t.scsj.net.cn/admin';
    var addressId = $.cookie('addressId');
    var tk = $.cookie('tk');
    var uid = $.cookie('uid');
    $('.sub').click(function () {
        var name = $('.name').val();
        var phone = $('.phone').val();
        var area = $('.area').val();
        var detail = $('.detailArea').val();
        var dataJson = '',url="";
        var arr = $("#value1").val().split(",");
        var provinceId = arr[0];
        var cityId = arr[1]?arr[1]:"";
        var countyId = arr[2]?arr[2]:"";
        if (addressId) {
            dataJson = {
                tk: tk,
                id: addressId,
                provinceId: provinceId,
                cityId: cityId,
                countyId: countyId,
                areaInfo: area,
                address: detail,
                userId: uid,
                phone: phone,
                name: name,
                isDefault: true
            };
            url = commonHost+"/index.php/my/edit_address";
        } else {
            dataJson = {
                tk: tk,
                provinceId: provinceId,
                cityId: cityId,
                countyId: countyId,
                areaInfo: area,
                address: detail,
                userId: uid,
                phone: phone,
                name: name,
                isDefault: true
            };
            url = commonHost+"/index.php/my/add_address";
        };
        editAddress(dataJson, url);
    })

    // 设置收货地址函数封装
    function editAddress(data, url) {
        $.ajax({
            type: 'post',
            url: url,
            data: data,
            success: function (data) {
                data = JSON.parse(data);
                if(data.ret==1){
                    location.href = "./confirmOrder.html";
                }else {
                    $("body").toast({
                        content:data.msg
                    })
                }
            },
            error: function () {
                alert('系统错误')
            }
        })
    }
})